package com.lungle.warmdiary.app.view.active.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.utils.StringUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ImageUpdateAdapter extends BaseAdapter {

    private List<String> list;
    private Context mContext;

    public ImageUpdateAdapter(Context context, List<String> list) {
        this.list = list;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;

        if (convertView == null || convertView.getTag() == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(
                    R.layout.item_picture_upload, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        if (!TextUtils.isEmpty(list.get(position)))

            ImageLoader.getInstance().loadImage(StringUtils.getPreviewImageUrl(list.get(position)), new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    vh.mIvActive.setImageBitmap(loadedImage);
                }
            });
        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.iv_active_img)
        ImageView mIvActive;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}